package com.emm.yixun.service;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.emm.yixun.mobile.application.EmmApplication;

public class MyService extends Service {

	private static final String TAG = "MyService";

	/** 获取网络状态 */
	private final int NETWORKTYPE = 0x101;

	/** 网络状态标识 WIFI */
	private final int NETTYPE_WIFI = 0x01;

	/** 网络状态标识 WAP */
	private final int NETTYPE_CMWAP = 0x02;

	/** 网络状态标识 NET */
	private final int NETTYPE_CMNET = 0x03;

	/** 是否继续检测标识 */
	private boolean ISDETECTION = true;

	/** 继续检测 */
	private final String ACTION1 = "com.yixuninfo.emm.true";

	/** 停止检测 */
	private final String ACTION2 = "com.yixuninfo.emm.false";

	/** 打开状态提示 */
	private final String ACTION3 = "com.yixuninfo.emm.open_status";

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == NETWORKTYPE) {// 接收到指令，开始每10秒检测一次网络状态
				if (ISDETECTION) {
					int NETTYPE = getNetworkType();// 得到当前网络状态类型
					Log.d(TAG, "NETWORKTYPE:" + GetContentType(NETTYPE));// 输出到控制台
				}
				Message message = handler.obtainMessage(NETWORKTYPE);// 为下一次监测操作做排队
				handler.sendMessageDelayed(message, 10000);
			}
		};
	};

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		handler.sendEmptyMessage(NETWORKTYPE);// 开始发起检测网络状态操作
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onStart(Intent intent, int startId) {// 接收广播指令
		IntentFilter mfilter = new IntentFilter();
		mfilter.addAction(ACTION1);
		mfilter.addAction(ACTION2);
		mfilter.addAction(ACTION3);
		registerReceiver(MyGetInstructions, mfilter);
		super.onStart(intent, startId);
	}

	/**
	 * 获取到广播指令
	 */
	BroadcastReceiver MyGetInstructions = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (ACTION1.equals(action)) {// 更改检测输出状态
				ISDETECTION = true;
			} else if (ACTION2.equals(action)) {
				ISDETECTION = false;
			} else if (ACTION3.equals(action)) {
				SetToast(getNetworkType());
			}
		}
	};

	/**
	 * 获取当前网络类型
	 *
	 * @return 0：没有网络 1：WIFI网络 2：WAP网络 3：NET网络
	 */
	public int getNetworkType() {
		int netType = 0;
		ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (networkInfo == null) {
			return netType;
		}
		int nType = networkInfo.getType();
		if (nType == ConnectivityManager.TYPE_MOBILE) {
			String extraInfo = networkInfo.getExtraInfo();
			if (!TextUtils.isEmpty(extraInfo)) {
				if (extraInfo.toLowerCase().equals("cmnet")) {
					netType = NETTYPE_CMNET;
				} else {
					netType = NETTYPE_CMWAP;
				}
			}
		} else if (nType == ConnectivityManager.TYPE_WIFI) {
			netType = NETTYPE_WIFI;
		}
		return netType;
	}

	private String GetContentType(int NETTYPE) {
		if (NETTYPE == 0) {
			return "没有网络";
		} else if (NETTYPE == 1) {
			return "WIFI";
		} else if (NETTYPE == 2) {
			return "WAP";
		} else if (NETTYPE == 3) {
			return "NET";
		} else {
			return "未知";
		}
	}

	/**
	 * <p>
	 * <b>Title：</b> 根据不同的网络状态显示提示
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 *
	 * @param NETTYPE
	 * @author tangxin
	 * @date 2017年1月11日 下午1:37:25
	 */
	private void SetToast(int NETTYPE) {
		if (NETTYPE == 0) {
			EmmApplication.Ts("请求失败,请检查设备网络");
		} else if (NETTYPE == 1 || NETTYPE == 2 || NETTYPE == 3) {
			EmmApplication.Ts("无法连接服务器,请稍后再试");
		} else {
			EmmApplication.Ts("请求失败,网络状态未知");
		}
	}

}
